package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"good/models"
	"strconv"
	"time"
)

type GoodController struct {
	beego.Controller
}

type JsonR struct {
	Code int         `json:"code"`
	Msd  string      `json:"msd"`
	Data interface{} `json:"data"`
}

func (c *GoodController) Get() {

}

func (c *GoodController) Upload() {
	file, header, err := c.GetFile("logo")
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msd:  "文件获取失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	if header.Size > 2*1024*1024 {
		c.Data["json"] = JsonR{
			Code: 0,
			Msd:  "上传的文件过大，请重新上传",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	defer file.Close()
	path := "static/img/" + header.Filename
	err = c.SaveToFile("logo", path)
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msd:  "文件上传失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		logosrc := "http://127.0.0.1:8080/" + path
		c.Data["json"] = JsonR{
			Code: 200,
			Msd:  "文件上传成功",
			Data: logosrc,
		}
		c.ServeJSON()
	}
}

func (c *GoodController) Add() {
	name := c.GetString("name")
	lx := c.GetString("lx")
	logo := c.GetString("logo")
	ms := c.GetString("ms")
	dpadd := c.GetString("dbadd")
	if name == "" || dpadd == "" {
		c.Data["json"] = JsonR{
			Code: 0,
			Msd:  "店铺名称与店铺地址不能为空",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	good := models.Good{
		Name:  name,
		Lx:    lx,
		Logo:  logo,
		Ms:    ms,
		Dpadd: dpadd,
	}
	err := models.DB.Create(&good).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msd:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msd:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}

func (c *GoodController) List() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 1)
	var count int64
	name := c.GetString("name")
	query := models.DB.Model(&models.Good{})
	pageDate := make(map[string]interface{})
	if name != "" {
		query = query.Where("name like ?", "%"+name+"%")
	}
	query.Count(&count)
	offset := (page - 1) * size
	key := "page-" + strconv.Itoa(page) + "-" + name + "-"
	val, err := models.Rdb.Get(key).Result()
	if err != nil {
		var good []models.Good
		err = query.Where(query).Offset(offset).Limit(size).Find(&good).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 0,
				Msd:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
		} else {
			pageDate["count"] = count
			pageDate["good"] = good
			jsonbyte, _ := json.Marshal(&pageDate)
			value := string(jsonbyte)
			models.Rdb.Set(key, value, time.Minute*3)
			c.Data["json"] = JsonR{
				Code: 200,
				Msd:  "mydsl-查询成功",
				Data: pageDate,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &pageDate)
		c.Data["json"] = JsonR{
			Code: 200,
			Msd:  "redis-查询成功",
			Data: pageDate,
		}
		c.ServeJSON()
	}

}

func (c *GoodController) Del() {
	id, _ := c.GetInt("id")
	err := models.DB.Where("id = ?", id).Delete(&models.Good{}, id).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msd:  "删除失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msd:  "删除成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}

func (c *GoodController) Info() {
	id, _ := c.GetInt("id")
	var good []models.Good
	key := "id-" + strconv.Itoa(id)
	val, err := models.Rdb.Get(key).Result()
	if err != nil {
		err = models.DB.Where("id = ?", id).First(&good).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 0,
				Msd:  "查询详情失败",
				Data: nil,
			}
			c.ServeJSON()
		} else {
			jsonbyte, _ := json.Marshal(&good)
			value := string(jsonbyte)
			models.Rdb.Set(key, value, time.Minute*3)
			c.Data["json"] = JsonR{
				Code: 200,
				Msd:  "mysql-查询详情成功",
				Data: good,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &good)
		c.Data["json"] = JsonR{
			Code: 200,
			Msd:  "redis-查询详情成功",
			Data: good,
		}
		c.ServeJSON()
	}

}
